<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Admin\Lianggoods;
use App\Admin\Categoods;
use App\Admin\GetCategory;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;

class CategoodsController extends Controller
{
    //分类列表显示/
    public function categoodsshow(Request $request){
        //根据搜索框是否传值来判断执行哪种查询方式
        if($request->id){
            $id = $request->id;
            if($id =='cateid'){
                $CateList = Categoods::where('pid','=',0)->get();
            }else{
                $Cate = Categoods::all();
                $getCate = new GetCategory();
                $CateList= $getCate->getTree($Cate,$id);
            }

        }else{
            //将分类数据库表遍历到后台
            $Cate = Categoods::all();
            $getCate = new GetCategory();
            $CateList= $getCate->getTree($Cate);
        }

        $searchlist = Categoods::where('pid','=',0)->get();
//        dd($search);

        return view('admin/categoods/CategoodsList',['CateList'=>$CateList,'searchlist'=>$searchlist]);
    }

    //添加分类
    public function categoodsadd(){
        //将分类数据库表遍历到后台
        $Cate = Categoods::all();
        //new getTree方法
        $getCate = new GetCategory();
//        var_dump($getCate);
        $CateAdd= $getCate->getTree($Cate);
//        dd($CateAdd);
        return view('admin/categoods/CategoodsAdd',['CateAdd'=>$CateAdd]);
    }
    //表单的验证添加
    public function categoodsadddata(Request $request){
//        dd($request);
        //验证规则
        $roles = [
            'name'=>'required|between:1,10',
        ];
        //自定义的错误信息
        $msg = [
            'name.required' => '良品购分类名称不能为空',
            'name.between'=>'良品购分类名称必须在:min和:max之间',
        ];
        $validator = Validator::make($request->all(), $roles, $msg);
        if ($validator->fails()) {
            //验证失败
            return redirect('admin/CategoodsAdd')->withErrors($validator);
        }
        $Category = new Categoods();
        $input = $request->all();
        $input['time']=time();
        $res = $Category::create($input);
        return redirect('admin/CategoodsList');
    }
    //修改分类
    public function categoodsedit(){
        //根据get传来的id将数据遍历到后台
        $id = $_GET['id'];
        $CateList = Categoods::where('id','=',$id)->get();
        //判断修改的该分类是否有子id
        $catelist = Categoods::where('pid','=',$id)->get();
        //给$result一个初始值 将遍历结果转为true OR false 来提供条件判断是否可以修改分类
        $result = '';
        foreach ($catelist as $item) {
            $result =($item->name);
        }
        if ($result != ''){
            $status = true;
        }else{
            $status = false;
        }
        //用getTree方法取到分类的所有名称以供更改
        $Cate = Categoods::all();
        $getCate = new GetCategory();
        $CateEdit= $getCate->getTree($Cate);

        return view('admin/categoods/CategoodsEdit',['CateList'=>$CateList,'status'=>$status,'CateEdit'=>$CateEdit,'id'=>$id]);
    }
    //提交修改表处理
    public function categoodseditdata(Request $request){
        //接收要修改的ID
        $id = $request->id;
        $pid = $request->ppid;
//        var_dump($id);
//        var_dump($pid);exit;
        //验证规则
        $roles = [
            'name'=>'required|between:1,10',
        ];
        //自定义的错误信息
        $msg = [
            'name.required' => '分类名称不能为空',
            'name.between'=>'分类名称必须在:min和:max之间',
        ];
        $validator = Validator::make($request->all(), $roles, $msg);
        if ($validator->fails()) {
            //验证失败
            return redirect('admin/CategoodsEdit?id='.$id.'&pid='.$pid)->withErrors($validator);
        }

        $input = Input::except('_token','ppid');
        $res = Categoods::where('id',$id)->update($input);
        return redirect('admin/CategoodsList');
    }
    //删除分类
    public function categoodsdel(){
        //见分类数据库表遍历到后台
        $id = $_GET['id'];
        //判断修改的该分类是否有子id
        $catelist = Categoods::where('pid','=',$id)->get();
        //给$result一个初始值 将遍历结果转为true OR false 来提供条件判断是否可以删除分类
        $result = '';
        foreach ($catelist as $item) {
            $result =($item->name);
        }
        if ($result != ''){
            $status = true;
        }else{
            $status = false;
        }
        if ($status == false){
            $single = Lianggoods::where('categoods_id','=',$id);
            $sing = Lianggoods::where('categoods_id','=',$id)->get()->toArray();
            if($sing){
                $ppic=$sing[0]['pic'];
                @unlink(public_path().'/adminliang'.'/'.$ppic);
                $singlist = $single->delete();
            }
//            dd($single);
            $Category = Categoods::find($id);
            $CateList = $Category->delete();
        }
        return redirect('admin/CategoodsList');
    }
}
